<!DOCTYPE html>
<html>
<head>
    <meta name="layout" content="main"/>
    <g:set var="entityName" value="${message(code: 'account.label', default: 'Account')}"/>
    <title>数据迁移</title>
    <asset:stylesheet src="ztree/demo.css"/>
    <asset:stylesheet src="ztree/zTreeStyle.css/"/>
    <asset:javascript src="ztree/jquery.ztree.core.js/"/>

    <script type="text/javascript">

        var setting = {
            view: {
                dblClickExpand: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                // beforeClick: beforeClick,
                onClick: onClick
            }
        };

        // 初始化数据
        initAccount();

        // 初始化数据
        function initAccount(name) {
            $.ajax({
                type: "post",
                url: "/dataMigration/getAllAccountList",
                data:{
                    name: name
                },
                success: function (data) {
                    if (data.status == "success") {
                        var accountList = data.accountList
                        $(document).ready(function(){
                            $.fn.zTree.init($("#treeDemo"), setting, accountList);
                            expandAll("treeDemo");
                        });
                    }
                },
                error: function () {
                    console.log("获取失败，请稍后重试");
                },
            });
        }

        function expandAll(treeId) {
           var zTree = $.fn.zTree.getZTreeObj(treeId);
           zTree.expandAll(true);
       }

        // function beforeClick(treeId, treeNode) {
        //     var check = (treeNode && !treeNode.isParent);
        //     if (!check) alert("只能选择城市...");
        //     return check;
        // }
        
        function onClick(e, treeId, treeNode) {
            $("#newAccountName").val(treeNode.name);
            $("#newAccountId").val(treeNode.id);
        }

        function showMenu() {
            var newAccountName = $("#newAccountName");
            var accountOffset = $("#newAccountName").offset();
            $("#menuContent").css({left:accountOffset.left + "px", top:accountOffset.top + newAccountName.outerHeight() + "px"}).slideDown("fast");

            $("body").bind("mousedown", onBodyDown);
        }
        function hideMenu() {
            $("#menuContent").fadeOut("fast");
            $("body").unbind("mousedown", onBodyDown);
        }
        function onBodyDown(event) {
            if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
                hideMenu();
            }
        }

        $(function(){
            $("#newAccountName").click(function() {
                showMenu();
            });

            $(".showDataMigration").click(function() {
                $("#newAccountId").val("");
                $("#newAccountName").val("");
                var oldAccountId = $(this).attr("dataId").trim();
                $("#oldAccountId").val(oldAccountId);
                $("#dataMigration").click();
            });
        })

    </script>
</head>

<body class="fixed-navbar fixed-sidebar">
<div class="small-header">
    <div class="hpanel">
        <div class="panel-body">
            <div id="hbreadcrumb" class="pull-right">
                <ol class="hbreadcrumb breadcrumb">
                    <li>CINDATA PLS</li>
                    <li class="active">
                        <span>数据迁移</span>
                    </li>
                </ol>
            </div>

            <h2 class="font-light m-b-xs">
                数据迁移
            </h2>
        </div>
    </div>
</div>

<div class="content animate-panel">
    <div class="row">
        <g:form method="POST" action="searchAccountData">
            <div class="hpanel hblue">
                <div class="panel-heading">
                    <div class="panel-tools">
                        <button class="btn btn-primary btn-xs" type="submit"><i class="fa fa-search"></i>查询</button>
                        <button class="btn btn-warning2 btn-xs" type="button" id="resetBtn"><i
                                class="fa fa-times"></i>重置</button>
                    </div>
                    查询
                </div>

                <div class="panel-body form-horizontal">
                    <div class="col-md-3 form-group">
                        <label class="col-md-4 lineLab text-right">机构名称</label>
                        <div class="col-md-8">
                            <input type="text" class="form-control" placeholder="机构名称" name="name" id="name" value="${params?.name}">
                        </div>
                    </div>
                    <div class="col-md-3 form-group">
                        <label class="col-md-4 lineLab text-right">机构联系人</label>
                        <div class="col-md-8">
                            <input type="text" class="form-control" placeholder="机构联系人" name="contact" id="contact" value="${params?.contact}">
                        </div>
                    </div>
                    <div class="col-md-3 form-group">
                        <label class="col-md-4 lineLab text-right">座机</label>
                        <div class="col-md-8">
                            <input type="text" class="form-control" placeholder="座机" name="phone" id="phone" value="${params?.phone}">
                        </div>
                    </div>
                    <div class="col-md-3 form-group">
                        <label class="col-md-4 lineLab text-right">电话</label>
                        <div class="col-md-8">
                            <input type="text" class="form-control" placeholder="电话" name="cellphone" id="cellphone" value="${params?.cellphone}">
                        </div>
                    </div>
                </div>
            </div>
        </g:form>
    </div>
    <div class="row">
        <div class="hpanel hgreen">
            <div class="panel-heading">
                <div class="panel-tools">
                    <!-- <g:link action="create" class="btn btn-info btn-xs"><i class="fa fa-plus"></i>新建</g:link> -->
                    <!-- <a class="showhide"><i class="fa fa-chevron-up"></i></a> -->
                </div>
                机构列表
            </div>
            <div class="panel-body no-padding">
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                        <tr>
                            <g:sortableColumn property="name" title="机构名称" params= "${params}" action="searchAccountData" class="col-md-1 text-center"/>
                            <g:sortableColumn property="contact" title="机构联系人" params= "${params}" action="searchAccountData" class="col-md-1 text-center"/>
                            <g:sortableColumn property="phone" title="座机" params= "${params}" action="searchAccountData" class="col-md-1 text-center"/>
                            <g:sortableColumn property="cellphone" title="电话" params= "${params}" action="searchAccountData" class="col-md-1 text-center"/>
                            <g:sortableColumn property="operation" title="操作" class="col-md-1 text-center"/>
                        </tr>
                        </thead>
                        <tbody>
                        <g:each in="${accountList}" status="i" var="it">
                            <tr>
                                <td class="text-center">${it?.name}</td>
                                <td class="text-center">${it?.contact}</td>
                                <td class="text-center">${it?.phone}</td>
                                <td class="text-center">${it?.cellphone}</td>
                                <td class="text-center">
                                    <a class="btn btn-xs btn-primary btn-outline hide" href="#myModal" data-toggle="modal" id="dataMigration">迁移</a>
                                    <a class="btn btn-xs btn-primary btn-outline showDataMigration" dataId="${it.id}">迁移</a>
                                </td>
                            </tr>
                        </g:each>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="panel-footer">
                <div class="pagination">
                    <g:paginate total="${accountCount ?: 0}" params="${params}"/>
                </div>
            </div>
        </div>
    </div>
</div>

%{--添加机构弹窗--}%
<div class="content animate-panel p-b-n">
    <div class="row">
        <div class="hpanel">
            <div class="modal fade hmodal-info" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="color-line"></div>
                        <div class="modal-header">
                            <h4 class="modal-title"><span id="title">数据迁移</span><small style="font-size: 14px" class="font-bold"> (注：<span style="color: red">*</span> 为必填)</small></h4>
                        </div>
                        <g:form action="dataMigration" class="form-horizontal" name="myForm">
                            <div class="modal-body form-horizontal">

                                <input type="hidden" name="oldAccountId" id="oldAccountId">
                                <input type="hidden" name="newAccountId" id="newAccountId">

                                <div class="form-group">
                                    <label class="col-md-2 control-label is-required">迁移机构</label>
                                    <div class="col-md-4">
                                        <input class="form-control" style="width: 200px" id="newAccountName" type="text" readonly value=""/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div id="menuContent" class="menuContent">
                                        <ul id="treeDemo" class="ztree"></ul>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary" id="submit">迁移</button>
                            </div>
                        </g:form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<g:javascript>
    $(function () {

        $("#submit").click(function() {
            var newAccountName = $("#newAccountName").val();
            if(!newAccountName)
            {
                alert("请输入迁移机构！");
                return;
            }
            var oldAccountId = $("#oldAccountId").val();
            if(!oldAccountId)
            {
                alert("请选择迁移数据的原机构！");
                return;
            }
            var newAccountId = $("#newAccountId").val();
            if(!newAccountId)
            {
                alert("请选择迁移数据的目的机构！");
                return;
            }

            swal({
                title: "您确定迁移该数据吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                cancelButtonText: "取消",
                confirmButtonText: "确定",
                closeOnConfirm: false,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url:"/dataMigration/dataMigration",
                        type:"post",
                        data: {
                            oldAccountId: oldAccountId, 
                            newAccountId: newAccountId
                        },
                        success:function(data){
                            if(data.status == "success") {
                                swal("数据迁移成功！");
                                setTimeout(function() {
                                    window.location.href = "/dataMigration/index"
                                },2000);
                            } else {
                                swal(data.errorMessage, "", "error");
                            }
                        },
                        error:function(){
                            swal("服务器忙，数据迁移失败，请稍后重试！", "", "error");
                        },
                    });
                }
            });
            <!-- $("#submit").attr("type", "submit"); -->
        })

        //重置按钮
        $("#resetBtn").click(function () {
            $("#name").val("");
            $("#contact").val("");
            $("#phone").val("");
            $("#cellphone").val("");
        })
        $("#create").click(function (){
            location.href="create"
        })
    });
</g:javascript>

</body>
</html>
